//
//  ZYNTableViewCellForBase.m
//  VideoPic
//
//  Created by dllo on 16/3/25.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZYNTableViewCellForBase.h"
#import "ZYNModelForSongList.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
#define KWIDTH [UIScreen mainScreen].bounds.size.width
#define KHEIGHT [UIScreen mainScreen].bounds.size.height

@interface ZYNTableViewCellForBase()
@property (nonatomic, strong) UILabel *labelForSongName;
@property (nonatomic, strong) UILabel *labelForSingerName;
@property (nonatomic, strong) UILabel *labelForFavoriteCount;
@property (nonatomic, strong) ZYNModelForSongList *model;
@end
@implementation ZYNTableViewCellForBase

#pragma mark - init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}

#pragma mark - createSubViews
- (void)createSubViews {
    //cell style
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.labelOfCount = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfCount];

    self.labelForSongName = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForSongName];
    
    self.labelForSingerName = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForSingerName];
    
    self.labelForFavoriteCount = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelForFavoriteCount];
    
}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
  
    self.labelOfCount.frame = CGRectMake(20, 10, WIDTH / 10, WIDTH / 10);
    
    self.labelForSongName.frame = CGRectMake(10 + WIDTH / 10 + 10, 5, WIDTH - WIDTH / 6 - 70,  (HEIGHT - 15) / 2 );
    
    self.labelForSingerName.frame = CGRectMake(10 + WIDTH / 10 + 10, (HEIGHT - 15) / 2 + 5 + 5, [[self class] widthForLabelForSingerName:self.model.singerName], (HEIGHT - 15) / 2);
    self.labelForSingerName.font = [UIFont systemFontOfSize:15];
    
    self.labelForFavoriteCount.frame = CGRectMake([[self class] widthForLabelForSingerName:self.model.singerName] + 10 + WIDTH / 10 + 10 + 5, 5 + (HEIGHT - 15) / 2 + 5, WIDTH / 4, (HEIGHT - 15) / 2);
    self.labelForFavoriteCount.font = [UIFont systemFontOfSize:15];
   
}

+ (CGFloat)widthForLabelForSingerName:(NSString *)text {
    CGRect rect = [text boundingRectWithSize:CGSizeMake(0, KWIDTH / 14) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil];
    
    return rect.size.width;
}

- (void)passModel:(ZYNModelForSongList *)model {
    self.model = model;
    self.labelForSongName.text = model.name;
    self.labelForSingerName.text = model.singerName;
    NSString *str = [NSString stringWithFormat:@"%ld", model.favorites];
    if (str.length > 4) {
        CGFloat count = model.favorites;
        
        self.labelForFavoriteCount.text = [NSString stringWithFormat:@"♥️%.1f万", count / 10000];

    } else {
        
        self.labelForFavoriteCount.text = [NSString stringWithFormat:@"♥️%ld", model.favorites];
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
